常见异常处理1
2
3
4
5
6
7
8
9
10
11# 字典key空报错
dic = {}
print(dic["dashu"])# KeyError: 'dashu'
# 数组越界报错
lis = [1,2,3]
print(lis[4]) # IndexError: list index out of range
# 类型转换报错
s = "sd1"
print(int(s)) # ValueError: invalid literal for int() with base 10: 'sd1'
try-except-finally1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17# 当执行一个except后,不会执行之后的except
try:
float("casdq")
lis = [1,2]
lis[3]
dic = {}
dic["123"]
except ValueError as e:
print("ValueError",e.args)
except IndexError as e:
print("IndexError",e.args)
except Exception as e:
print("Exception",e.args)
finally :
print("结束啦")